﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Core.Extensions;
using ReceiptPrint.ViewModels.TemplateVMs;
using System.Linq;
using System.Text;
using WalkingTec.Mvvm.Mvc.Binders;

namespace ReceiptPrint.Controllers
{
    [AllRights]
    [ActionDescription("模板管理")]
    public partial class TemplateController : BaseController
    {
        #region Search
        [ActionDescription("Sys.Search")]
        public ActionResult Index(string id)
        {
            var vm = Wtm.CreateVM<TemplateListVM>();
            vm.Searcher.ExcelFileId = id;
            return PartialView(vm);
        }

        [ActionDescription("Sys.Search")]
        [HttpPost]
        public string Search(TemplateSearcher searcher)
        {
            var vm = Wtm.CreateVM<TemplateListVM>(passInit: true);
            if (ModelState.IsValid)
            {
                vm.Searcher = searcher;
                return vm.GetJson(false);
            }
            else
            {
                return vm.GetError();
            }
        }

        #endregion

        #region Create
        [ActionDescription("Sys.Create")]
        public ActionResult Create()
        {
            var vm = Wtm.CreateVM<TemplateVM>();
            vm.Entity.Header = ReportHelper.Head;
            vm.Entity.SubHeader = ReportHelper.Head2;
            vm.Entity.Footer = ReportHelper.Footer;
            return PartialView(vm);
        }

        [HttpPost]
        [ActionDescription("Sys.Create")]
        public ActionResult Create(TemplateVM vm)
        {
            if (!ModelState.IsValid)
            {
                return PartialView(vm);
            }
            else
            {
                vm.DoAdd();
                if (!ModelState.IsValid)
                {
                    vm.DoReInit();
                    return PartialView(vm);
                }
                else
                {
                    return FFResult().CloseDialog().RefreshGrid();
                }
            }
        }
        #endregion

        #region Edit
        [ActionDescription("Sys.Edit")]
        public ActionResult Edit(string id)
        {
            var vm = Wtm.CreateVM<TemplateVM>(id);
            return PartialView(vm);
        }

        [ActionDescription("Sys.Edit")]
        [HttpPost]
        [ValidateFormItemOnly]
        public ActionResult Edit(TemplateVM vm)
        {
            if (!ModelState.IsValid)
            {
                return PartialView(vm);
            }
            else
            {
                vm.DoEdit();
                if (!ModelState.IsValid)
                {
                    vm.DoReInit();
                    return PartialView(vm);
                }
                else
                {
                    return FFResult().CloseDialog().RefreshGridRow(vm.Entity.ID);
                }
            }
        }
        #endregion

        #region Delete
        [ActionDescription("Sys.Delete")]
        public ActionResult Delete(string id)
        {
            var vm = Wtm.CreateVM<TemplateVM>(id);
            return PartialView(vm);
        }

        [ActionDescription("Sys.Delete")]
        [HttpPost]
        public ActionResult Delete(string id, IFormCollection nouse)
        {
            var vm = Wtm.CreateVM<TemplateVM>(id);
            vm.DoDelete();
            if (!ModelState.IsValid)
            {
                return PartialView(vm);
            }
            else
            {
                return FFResult().CloseDialog().RefreshGrid();
            }
        }
        #endregion

        #region BatchDelete
        [HttpPost]
        [ActionDescription("Sys.BatchDelete")]
        public ActionResult BatchDelete(string[] IDs)
        {
            var vm = Wtm.CreateVM<TemplateBatchVM>(Ids: IDs);
            return PartialView(vm);
        }

        [HttpPost]
        [ActionDescription("Sys.BatchDelete")]
        public ActionResult DoBatchDelete(TemplateBatchVM vm, IFormCollection nouse)
        {
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return PartialView("BatchDelete",vm);
            }
            else
            {
                return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.BatchDeleteSuccess", vm.Ids.Length]);
            }
        }
        #endregion
        [ActionDescription("商品详情")]
        public ActionResult ReceiptInfo(string id)
        {
            return Redirect($"~/ReceiptInfo/Index?id={id}");
        }
        [ActionDescription("打印")]
        public ActionResult Print(string id)
        {
            var vm = Wtm.CreateVM<TemplateVM>(id);
            var cid = id.ToLong();
            var subs = DC.Set<Models.ReceiptInfo>().Where(x=>x.TemplateId == cid).ToList();

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine(vm.Entity.Header);
            stringBuilder.AppendLine(ReportHelper.GetLine());
            stringBuilder.AppendLine(vm.Entity.SubHeader);
            stringBuilder.AppendLine(ReportHelper.GetLine());
            stringBuilder.AppendLine(ReportHelper.GetERU());
            stringBuilder.AppendLine(ReportHelper.GetReceiptInfo(subs));

            stringBuilder.AppendLine(ReportHelper.GetBelastingtariefAndMoney(vm.Entity.Money,subs));
            
            stringBuilder.AppendLine(vm.Entity.Footer);

            vm.Info = stringBuilder.ToString();

            return PartialView(vm);
        }

        [ActionDescription("打印")]
        [HttpPost]
        [ValidateFormItemOnly]
        [StringNeedLTGT]
        public ActionResult Print(TemplateVM vm)
        {
            ReportHelper.Print(vm.Entity.ID,vm.Info);
            return FFResult().Message("打印命令已发送");
        }


    }
}
